/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Setari.java
 *
 * Created on Jun 29, 2010, 7:00:39 PM
 */

package point.of.sale.app;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author card3r
 */
public class Setari extends javax.swing.JFrame {

    /** Creates new form Setari */
    String server,username,passwd,dbase;
    Connection conn;
    public Setari() {
        try {
            String executionPath = System.getProperty("user.dir");
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:" + executionPath + "//config//settings");
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery("select * from settings");
            while (rs.next()) {
                server = rs.getString("server");
                username = rs.getString("user");
                passwd = rs.getString("pass");
                dbase = rs.getString("db");
            }
            
            initComponents();
            this.ServerTextField.setText(server);
            this.UserTextField.setText(username);
            this.PasswordTextField.setText(passwd);
            this.BdTextField.setText(dbase);
        } catch (SQLException ex) {
            Logger.getLogger(Setari.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.toString());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Setari.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.toString());
        }
    }
    public void modificaSetari(String serv,String usr,String paswd,String dbs)
    {
        try {
            //String query = "UPDATE settings SET server = '"+serv+"', SET user = '"+usr+"', SET pass = '"+paswd+
              //      "', SET db = '"+dbs+"' WHERE id = 1";
            String query1 = "UPDATE settings SET server = '"+serv+"' WHERE id=1";
            String query2 = "UPDATE settings SET user = '"+usr+"' WHERE id=1";
            String query3 = "UPDATE settings SET pass = '"+paswd+"' WHERE id=1";
            String query4 = "UPDATE settings SET db = '"+dbs+"' WHERE id=1" ;
            PreparedStatement ps1 = this.conn.prepareStatement(query1);
            ps1.executeUpdate();
            PreparedStatement ps2 = this.conn.prepareStatement(query2);
            ps2.executeUpdate();
            ps2.close();
            PreparedStatement ps3 = this.conn.prepareStatement(query3);
            ps3.executeUpdate();
            PreparedStatement ps4 = this.conn.prepareStatement(query4);
            ps4.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(Setari.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.toString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ServerTextField = new javax.swing.JTextField();
        UserTextField = new javax.swing.JTextField();
        PasswordTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        BdTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Server");

        jLabel2.setText("User");

        jLabel3.setText("Password");

        jLabel4.setText("Baza Date");

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(BdTextField)
                            .addComponent(PasswordTextField)
                            .addComponent(UserTextField)
                            .addComponent(ServerTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ServerTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UserTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PasswordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.modificaSetari(this.ServerTextField.getText(), this.UserTextField.getText(), this.PasswordTextField.getText(), this.BdTextField.getText());
        this.setVisible(false);
        this.dispose();
        JOptionPane.showMessageDialog(null, "RESTART PENTRU SALVAREA SETARILOR");
        System.exit(-666);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void mainSetari() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Setari().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField BdTextField;
    private javax.swing.JTextField PasswordTextField;
    private javax.swing.JTextField ServerTextField;
    private javax.swing.JTextField UserTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables

}
